package com.hqyj.system.controller;

import com.hqyj.system.model.Purchase;
import com.hqyj.system.model.Stock;
import com.hqyj.system.model.User;
import com.hqyj.system.model.UserPurchase;
import frame.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/purchaseController")
public class PurchaseController  extends BaseController {
  @RequestMapping("/list.do")
  public String List(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user,Purchase purchase) {
      //1.接收数据;
      //3.调用业务;
      List<Purchase> purchases_dbList = purchaseService.queryAllPuchase();
      //返回数据
      model.addAttribute("purchases_dbList", purchases_dbList);
      //2.跳转页面;
      return "view/system/purchase/purchaseList";
  }
    @RequestMapping("/addUI.do")
    public String addUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Purchase purchase) {
        //1.接收数据;
        //3.调用业务;
        //返回数据
        model.addAttribute("purchase", purchase);
        //2.跳转页面;
        return "view/system/purchase/add";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Purchase purchase) {
        //1.接收数据;
        //3.调用业务;
        int n=purchaseService.addPurchaseByPurchase(purchase);
        if(n>0){
            User user = (User)session.getAttribute("USER");
            UserPurchase userPurchase = new UserPurchase();
            userPurchase.setPurchaseCode(purchase.getPurchaseCode());
            userPurchase.setUserId(user.getUserId());
            int n2 = userPurchaseService.addUserPurchaseByUserPurchase(userPurchase);
        }
        return "forward:/stockController/list.do";
    }
}
